package com.refusea.commons.media

import org.jcodec.api.FrameGrab
import org.jcodec.scale.AWTUtil
import java.io.ByteArrayOutputStream
import java.io.File
import javax.imageio.ImageIO


/**
 * @author Zhang Yan, 2023/10/10
 **/
object Videos {

    private const val BUFFER_SIZE: Int = 128 * 1024

    @JvmStatic
    fun getFrame(file: File, format: String = JPEG, number: Int = 0): ByteArray {
        val bufferedImage = AWTUtil.toBufferedImage(FrameGrab.getFrameFromFile(file, number))
        val output = ByteArrayOutputStream(BUFFER_SIZE)
        ImageIO.write(bufferedImage, format, output)
        return output.toByteArray()
    }

}
